#!/bin/bash 
#editor by yhzong
#date:20200727
#手动输入zabbix_server端ip地址
read -p "请输入zabbixserver端ip地址 > " zabbix_server_ip
OS=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
myip=`ifconfig |grep "inet"|grep -v "127.0.0.1"|awk '{print $2}'`
#检测服务器是否可以上网，如果不能上网退出安装
echo "检查网络是否可以访问外网,不能访问外网将停止脚本运行"
check_net=`curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com`
if [ ${check_net} -ne 200 ];then
	echo "请检查网络是否正常，能否访问外网"
	exit 1
fi
#安装替换yum源的wget软件
yum install wget -y > /dev/null 2>&1
#替换镜像源为阿里镜像源
echo "更换为阿里云yum源"
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
if [ $OS == '7' ];then
	wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo > /dev/null 2>&1
	wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo >/dev/null 2>&1
	sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo
elif [ $OS == '6' ];then
	wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
        wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
	sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo
else 
	exit 0
fi
	
#下载zabbixagent并安装
if [ $OS == '7' ];then
	wget https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.15-1.el7.x86_64.rpm
	yum install -y zabbix-agent-3.4.15-1.el7.x86_64.rpm
elif [ $OS == '6' ];then
	 wget https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/6/x86_64/zabbix-agent-3.4.15-1.el6.x86_64.rpm
         yum install -y zabbix-agent-3.4.15-1.el6.x86_64.rpm
else
	exit 0
fi

#配置zabbixserver端地址

sed -i '/^Server=/c Server='${zabbix_server_ip}'' /etc/zabbix/zabbix_agentd.conf
sed -i '/^ServerActive=/c ServerActive='${zabbix_server_ip}'' /etc/zabbix/zabbix_agentd.conf
sed -i '/^Hostname=/c Hostname='${myip}'' /etc/zabbix/zabbix_agentd.conf

#启动zabbix-agent
if [ $OS == '7' ];then
	systemctl start zabbix-agent
elif [ $OS == '6' ];then
	service zabbix-agent start
else
	exit 0
fi

#检测zabbix-agent是否启动
zabbix_status=`netstat -nltp |grep "10050"`
if [ -z "${zabbix_status}" ];then
	echo "zabbix-agent installed  failure！！！"
else
	echo “zabbix-agent successful installed ！！！”
fi

